/*
 * StockTableView.cpp
 *
 *  Created on: 10.04.2009
 *      Author: Konan Andrej
 */

#include "StockTableView.h"

StockTableView::StockTableView( DataBase * aDB, QWidget * parent ) : QTableView( parent )
{
        //Print("StockTableView() constructor is executed");
	DB = aDB;
        connect( DB, SIGNAL(ItemAdded()), this, SLOT(RowInserted()));
        connect( DB, SIGNAL(ItemRemoved()), this, SLOT(RowRemoved()));

	stockModel = new StockModel( DB, this );
	setModel( stockModel );

	resizeRowsToContents();      
        VarifyColumnWidth();

	setSelectionMode( QAbstractItemView::SingleSelection );
	setSelectionBehavior( QAbstractItemView::SelectRows );

	SelectedRow = -1;

        //connect( this, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(RowSelected(const QModelIndex&)) );

}

StockTableView::~StockTableView()
{
    delete stockModel;
}

void StockTableView::SetDatabase( DataBase *db )
{
    DB = db;
    stockModel->SetDatabase( DB );
}

void StockTableView::mousePressEvent( QMouseEvent * event )
{
	//Print( "mousePressEvent() for StockTableView is executed" );
	int RowValue = rowAt( event->y() );
	if (RowValue != -1){
		SelectedRow = RowValue;
		selectRow( SelectedRow );
	} else {
		SelectedRow = RowValue;
		clearSelection();
	}
}

void StockTableView::mouseDoubleClickEvent( QMouseEvent *event )
{
    //Print("Double-click");
    //Print(QString("Selected row: ") + QString::number(SelectedRow) );
    GetSelectedItem();
}

int StockTableView::InsertRow( int row )
{
	//Print("InsertRow() is executed");

        model()->insertRows(row, 0);

	clearSelection();
	SelectedRow = -1;

        //InsertRow is executed before resetModel,
        //error occupies when executing data
        //resizeRowsToContents();

	return 0;
}

int StockTableView::GetSelectedItem( ItemType& item )
{
	if ( SelectedRow < 0 || SelectedRow >= DB->GetItemsCount() ) return 1;

        //DB->GetItem( SelectedRow, item );
        stockModel->GetItem( SelectedRow, item );
	return 0;
}

void StockTableView::GetSelectedItem()
{
    ItemType item;
    //Print( QString::number(GetSelectedItem( item )) );
    if ( GetSelectedItem( item ) == 1 )
        emit NoItemIsSelected(); else
            emit Item( item );
}

int StockTableView::getItem(int index, ItemType &item)
{
    return stockModel->GetItem( index, item );
}

int StockTableView::RemoveRow( int row )
{
	//Print( "RemoveRow() is executed" );

        model()->removeRows(row, 0);

	clearSelection();
	SelectedRow = -1;

	return 0;
}

void StockTableView::RowInserted()
{
    InsertRow( 0 );
}

void StockTableView::RowRemoved()
{
    RemoveRow( 0 );
}

void StockTableView::VarifyColumnWidth()
{
    int sum = 0;
    for (int i=0; i<DB->GetVisibleNumber(); i++){
        setColumnWidth(i, DB->GetColumnWidth( i ) );
        sum += DB->GetColumnWidth( i );
    }

    setMinimumSize( sum + MarginSpace, 500 );
}

void StockTableView::FindItem( const QString &str )
{
    int res = DB->GetItemIndex( str );
    //Print( QString("index of search: ") + QString::number( res ) );
    if ( res == -1 ) emit ItemNotFound(); else
        {
            SelectedRow = DB->GetItemIndex( str );
            selectRow( SelectedRow );
            emit ItemFound();
        }
}

